﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShopFine.Models
{
    public class SubCategoryDAO
    {
        ShopFineEntities db;

        public SubCategoryDAO()
        {
            db = new ShopFineEntities();
        }

        public List<SubCategory> GetAll()
        {
            return db.SubCategories.ToList();
        }

        public List<SubCategory> GetSubCategoriesByCategoryID(int categoryID)
        {
            return db.SubCategories.Where(p => p.CategoryID == categoryID).ToList();
        }

        internal bool CheckCateSelected(int cateID, int subID)
        {
            var cate = db.Categories.FirstOrDefault(p=>p.ID.Equals(cateID));
            return cate == null ? false : (cate.SubCategories.Any(p => p.ID.Equals(subID)) ? true : false);
        }

        internal bool CheckSubCateSelected(int subCateCheck, int currentSubCate)
        {
            var subCate = db.SubCategories.FirstOrDefault(p => p.ID.Equals(currentSubCate));
            int cateID = subCate == null ? -1 : subCate.CategoryID.Value;
            var cate = db.Categories.FirstOrDefault(p => p.ID.Equals(cateID));
            return cate == null ? false : (cate.SubCategories.Any(p => p.ID.Equals(subCateCheck)) ? true : false);
        }

        internal SubCategory GetSubCategoryByID(int subCateID)
        {
            return db.SubCategories.FirstOrDefault(p => p.ID == subCateID);
        }
    }
}